from datetime import datetime
from enum import Enum, auto


class LogLevel(Enum):
    INFO = auto()
    DEBUG = auto()
    WARNING = auto()    # 告警，不影响后续运行
    ERROR = auto()      # 出错了，只影响当前用户
    FATAL = auto()      # 致命错误，服务挂掉了


class TimeUtil:
    @staticmethod
    def get_time_stamp():
        return datetime.now().strftime("%Y-%m-%d %H:%M:%S")


class Logger:
    @staticmethod
    def log(level: LogLevel, file_name: str, line: int):
        # 构建日志前缀
        message = f"[{level.name}][{file_name}][{line}][{TimeUtil.get_time_stamp()}]"
        print(message, end='')  # 不换行，允许后续消息追加
        return Logger.MessageWrapper()


    class MessageWrapper:
        def __lshift__(self, msg):
            print(msg, end='')
            return self


# 使用宏类似的简洁调用方式
def LOG(level):
    import inspect
    # 获取调用处的文件名和行号
    frame = inspect.currentframe().f_back
    file_name = frame.f_code.co_filename.split('/')[-1]  # 只取文件名
    line = frame.f_lineno
    return Logger.log(level, file_name, line)


# 使用示例
if __name__ == "__main__":
    LOG(LogLevel.INFO) << "这是一条信息日志" << "\n"
    LOG(LogLevel.DEBUG) << "调试变量值: " << 42 << "\n"
    LOG(LogLevel.WARNING) << "这是一个警告" << "\n"
    LOG(LogLevel.ERROR) << "发生了错误" << "\n"
    LOG(LogLevel.FATAL) << "致命错误，服务即将终止" << "\n"